package com.dlc.shop.user.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.user.common.dao.UserLevelLangMapper;
import com.dlc.shop.user.common.model.UserLevel;
import com.dlc.shop.user.common.model.UserLevelLang;
import com.dlc.shop.user.common.service.UserLevelLangService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户等级国际化
 *
 * @author LGH
 * @date 2022-12-19 10:01:29
 */
@Service
@AllArgsConstructor
public class UserLevelLangServiceImpl extends ServiceImpl<UserLevelLangMapper, UserLevelLang> implements UserLevelLangService {

    private final UserLevelLangMapper userLevelLangMapper;

    @Override
    public void saveLangByLevel(UserLevel userLevel) {
        if (userLevel == null) {
            return;
        }
        // 保存会员等级名称国际化信息
        List<UserLevelLang> langs = this.buildLangsByLevel(userLevel);
        this.saveBatch(langs);
    }

    @Override
    public void updateLangByLevel(UserLevel userLevel) {
        if (userLevel == null) {
            return;
        }
        List<UserLevelLang> langs = this.list(new LambdaQueryWrapper<UserLevelLang>()
                .eq(UserLevelLang::getLevelId, userLevel.getId()));
        for (UserLevelLang lang : langs) {
            lang.setLevelName(lang.getLang() == 0 ? userLevel.getLevelNameCn() : userLevel.getLevelNameEn());
        }
        this.updateBatchById(langs);
    }

    @Override
    public void deleteByLevelId(Long levelId) {
        this.remove(new LambdaQueryWrapper<UserLevelLang>()
                .eq(UserLevelLang::getLevelId, levelId));
    }

    @Override
    public boolean checkNameRepeatByLevel(UserLevel userLevel) {
        if (userLevel == null) {
            return false;
        }
        Long levelId = userLevel.getId();
        Long shopId = userLevel.getShopId();
        Integer levelType = userLevel.getLevelType();
        // 查出该店铺其余会员等级的中英名称
        LambdaQueryWrapper<UserLevelLang> wrapper = new LambdaQueryWrapper<UserLevelLang>()
                .eq(UserLevelLang::getShopId, shopId)
                .eq(UserLevelLang::getLevelType, levelType);
        if (levelId != null) {
            wrapper.ne(UserLevelLang::getLevelId, userLevel.getId());
        }
        List<UserLevelLang> langs = this.list(wrapper);
        // 检查重名
        return this.checkNameRepeat(langs, userLevel);
    }

    /**
     * 构建中英语言集合（通过会员等级）
     * @param userLevel
     * @return
     */
    private List<UserLevelLang> buildLangsByLevel(UserLevel userLevel) {
        List<UserLevelLang> langs = new ArrayList<>(2);
        UserLevelLang lang;
        for (int i=0; i<userLevel.getUserLevelLangList().size(); i++) {
            lang = new UserLevelLang();
            lang.setShopId(userLevel.getShopId());
            lang.setLevelId(userLevel.getId());
            lang.setLevelType(userLevel.getLevelType());
            lang.setLang(i);
            lang.setLevelName(i == 0 ? userLevel.getLevelNameCn() : userLevel.getLevelNameEn());
            langs.add(lang);
        }
        return langs;
    }

    /**
     * 检查名称重复
     * @param langs 国际化名称集合
     * @param userLevel 会员等级
     * @return
     */
    private boolean checkNameRepeat(List<UserLevelLang> langs,
                                    UserLevel userLevel) {
        if (CollectionUtils.isEmpty(langs)
                || userLevel == null) {
            return false;
        }
        // 按语言进行map分组，并取出名称
        Map<Integer, List<String>> langNamesMap = new HashMap<>(2);
        List<String> langNames;
        for (UserLevelLang lang : langs) {
            langNames = langNamesMap.getOrDefault(lang.getLang(), new ArrayList<>());
            langNames.add(lang.getLevelName());
            langNamesMap.put(lang.getLang(), langNames);
        }
        // 检查中英名称重复
        String levelName;
        for (int i=0; i< langs.size(); i++) {
            levelName = i == 0 ? userLevel.getLevelNameCn() : userLevel.getLevelNameEn();
            langNames = langNamesMap.get(i);
            if (!ObjectUtils.isEmpty(levelName)
                    && langNames != null
                    && langNames.contains(levelName)) {
                return true;
            }
        }
        return false;
    }
}
